package com.techsen.tsweb.sys.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.techsen.tsweb.core.domain.JsonResult;
import com.techsen.tsweb.core.util.ValidUtil;
import com.techsen.tsweb.core.util.sql.Pager;
import com.techsen.tsweb.sys.domain.Company;
import com.techsen.tsweb.sys.domain.OrgRule;
import com.techsen.tsweb.sys.service.OrgRuleService;

@Controller
@RequestMapping("/sys/orgrule")
public class OrgRuleController {

    @Resource
    private OrgRuleService orgRuleService;

    /**
     * 根据父组织类型或子组织类型查询组织规则集合
     */
    @RequestMapping("/list")
    public @ResponseBody List<OrgRule> list(String parentOrgType, String childOrgType) {
        List<OrgRule> list = new ArrayList<OrgRule>();
        if (ValidUtil.isValid(parentOrgType) || ValidUtil.isValid(childOrgType)) {
            list = this.orgRuleService.getListByEntity(
                    new OrgRule().setParentOrgType(parentOrgType).setChildOrgType(childOrgType)
                    );
        } else {
            String company = Company.class.getSimpleName().toLowerCase();
            list = this.orgRuleService.getListByEntity(
                    new OrgRule().setParentOrgType(company).setChildOrgType(company)
                    );
        }
        return list;
    }
    
    /**
     * 进入组织规则管理界面
     */
    @RequestMapping
    public String index(String menuId, Model model) {
        model.addAttribute("menuId", menuId);
        return "sys/orgrule";
    }
    
    /**
     * 分页查询组织规则
     */
    @RequestMapping("/pager")
    public @ResponseBody Pager<OrgRule> pager(@RequestBody Pager<OrgRule> pager) {
        return this.orgRuleService.findByPager(pager);
    }
    
    /**
     * 进入添加组织规则界面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return "sys/orgrule-add";
    }
    
    /**
     * 添加组织规则
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody JsonResult add(OrgRule orgrule) {
        this.orgRuleService.add(orgrule);
        return new JsonResult(orgrule);
    }
    
    /**
     * 删除组织规则
     */
    @RequestMapping("/delete")
    public @ResponseBody JsonResult delete(String id) {
        this.orgRuleService.delete(id);
        return new JsonResult(id);
    }
    
    /**
     * 进入修改组织规则界面
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(String orgruleId, Model model) {
        OrgRule orgrule = this.orgRuleService.get(orgruleId);
        model.addAttribute("orgrule", orgrule);
        return "sys/orgrule-update";
    }
    
    /**
     * 修改组织规则
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public @ResponseBody JsonResult update(OrgRule orgrule) {
        this.orgRuleService.update(orgrule);
        return new JsonResult(orgrule);
    }

}
